﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data;
using MySql.Data.MySqlClient;
using BL;

namespace StockVentas
{
    public partial class frmArticulosItems : Form
    { 
        private DataTable tblArticulosItems;
        private DataView viewArticulosItems;
        string accion;

        public enum FormState
        {
            inicial,
            edicion,
            insercion,
            eliminacion
        }

        public frmArticulosItems()
        {
            InitializeComponent();
            Utilitarios.AddEventosABM(grpCampos, ref btnGrabar, ref chkActivo);
        }

        private void frmArticulosItems_Load(object sender, EventArgs e)
        {
            tblArticulosItems = BL.GetDataBLL.ArticulosItems();
            tblArticulosItems.PrimaryKey = new DataColumn[] { tblArticulosItems.Columns["IdItemITE"] };
            viewArticulosItems = new DataView(tblArticulosItems);
            gvwDatos.DataSource = viewArticulosItems;
            gvwDatos.Columns["IdItemITE"].HeaderText = "Nº Item";
            gvwDatos.Columns["DescripcionITE"].HeaderText = "Descripción";
            gvwDatos.Columns["DescripcionWebITE"].Visible = false;
            gvwDatos.Columns["ActivoWebITE"].Visible = false;
            SetStateForm(FormState.inicial);            
        }

        private void gvwDatos_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (gvwDatos.CurrentRow != null && gvwDatos.CurrentRow.DataBoundItem != null)
            {
                txtIdItem.Text = ((DataRowView)gvwDatos.CurrentRow.DataBoundItem)["IdItemITE"].ToString();
                txtDescripcion.Text = ((DataRowView)gvwDatos.CurrentRow.DataBoundItem)["DescripcionITE"].ToString();
                txtDescripcionWeb.Text = ((DataRowView)gvwDatos.CurrentRow.DataBoundItem)["DescripcionWebITE"].ToString();
                chkActivo.Checked = Convert.ToBoolean(((DataRowView)gvwDatos.CurrentRow.DataBoundItem)["ActivoWebITE"].ToString());
            }
        }

        private void btnBuscar_Click(object sender, EventArgs e)
        {
            string parametros = this.txtParametros.Text;
            viewArticulosItems.RowFilter = "DescripcionITE LIKE '" + parametros + "*'";
        }

        private void btnNuevo_Click(object sender, EventArgs e)
        {
            accion = "nuevo";
            SetStateForm(FormState.insercion);
        }

        private void btnEditar_Click(object sender, EventArgs e)
        {
            accion = "editar";
            SetStateForm(FormState.edicion);
        }

        private void btnGrabar_Click(object sender, EventArgs e)
        {
            string strDescripcion = txtDescripcion.Text.ToUpper();
            string strDescripcionWeb = txtDescripcionWeb.Text.ToUpper();

            int intActivoWeb;
            if (chkActivo.CheckState.ToString() == "Checked")
            {
                intActivoWeb = 1;
            }
            else
            {
                intActivoWeb = 0;
            }              
            if (accion == "nuevo")
            {
                Random rand = new Random();
                int clave = rand.Next(1, 999);
                bool existe = true;
                while (existe == true)
                {
                    DataRow foundRow = tblArticulosItems.Rows.Find(clave);
                    if (foundRow == null)
                    {
                        existe = false;
                    }
                    else
                    {
                        clave = rand.Next(1, 999);
                    }
                }          
                DataRowCollection cfilas = tblArticulosItems.Rows;
                DataRow nuevaFila;
                nuevaFila = tblArticulosItems.NewRow();
                nuevaFila[0] = clave;
                nuevaFila[1] = strDescripcion;
                nuevaFila[2] = strDescripcionWeb;
                nuevaFila[3] = intActivoWeb;
                cfilas.Add(nuevaFila);
                if (gvwDatos.CurrentRow != null && gvwDatos.CurrentRow.DataBoundItem != null)
                {
                    txtIdItem.Text = ((DataRowView)gvwDatos.CurrentRow.DataBoundItem)["IdItemITE"].ToString();
                    txtDescripcion.Text = ((DataRowView)gvwDatos.CurrentRow.DataBoundItem)["DescripcionITE"].ToString();
                }
                gvwDatos.Sort(gvwDatos.Columns["DescripcionITE"], ListSortDirection.Ascending);
            }
            else  //EDICION
            {
                DataRowView vistaFilaActual = (DataRowView)gvwDatos.CurrentRow.DataBoundItem;
                vistaFilaActual.BeginEdit();
                vistaFilaActual["DescripcionITE"] = strDescripcion;
                vistaFilaActual["DescripcionWebITE"] = strDescripcionWeb;
                vistaFilaActual["ActivoWebITE"] = intActivoWeb;
                vistaFilaActual.EndEdit();
            }
            SetStateForm(FormState.inicial);
        }

        private void btnBorrar_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("¿Desea borrar este registro?", "Buscar", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                gvwDatos.Rows.Remove(gvwDatos.CurrentRow);
            }
        }

        private void btnCancelar_Click(object sender, EventArgs e)
        {
            if (gvwDatos.CurrentRow != null && gvwDatos.CurrentRow.DataBoundItem != null)
            {
                txtIdItem.Text = ((DataRowView)gvwDatos.CurrentRow.DataBoundItem)["IdItemITE"].ToString();
                txtDescripcion.Text = ((DataRowView)gvwDatos.CurrentRow.DataBoundItem)["DescripcionITE"].ToString();
                txtDescripcionWeb.Text = ((DataRowView)gvwDatos.CurrentRow.DataBoundItem)["DescripcionWebITE"].ToString();
            }
            SetStateForm(FormState.inicial);
        }

        private void btnSalir_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void frmArticulosItems_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (tblArticulosItems.GetChanges() != null)
            {
                frmProgress progreso = new frmProgress(tblArticulosItems, "frmArticulosItems", "grabar");
                progreso.ShowDialog();
            }
        }

        public void SetStateForm(FormState state)
        {

            if (state == FormState.inicial)
            {
                gvwDatos.Enabled = true;
                txtDescripcion.ReadOnly = true;
                txtDescripcionWeb.ReadOnly = true;
                chkActivo.Enabled = false;
                txtDescripcion.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
                txtDescripcionWeb.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
                btnBuscar.Enabled = true;
                btnNuevo.Enabled = true;
                btnEditar.Enabled = true;
                btnBorrar.Enabled = true;
                btnGrabar.Enabled = false;
                btnCancelar.Enabled = false;
                txtParametros.Focus();
            }

            if (state == FormState.insercion)
            {
                gvwDatos.Enabled = false;
                txtDescripcion.ReadOnly = false;
                txtDescripcionWeb.ReadOnly = false;
                chkActivo.Enabled = true;
                txtIdItem.Clear();
                txtDescripcion.Clear();
                txtDescripcion.Focus();
                btnBuscar.Enabled = false;
                btnNuevo.Enabled = false;
                btnEditar.Enabled = false;
                btnBorrar.Enabled = false;
                btnGrabar.Enabled = false;
                btnCancelar.Enabled = true;
            }

            if (state == FormState.edicion)
            {
                gvwDatos.Enabled = false;
                txtDescripcion.ReadOnly = false;
                txtDescripcionWeb.ReadOnly = false;
                chkActivo.Enabled = true;
                txtDescripcion.Focus();
                btnBuscar.Enabled = false;
                btnNuevo.Enabled = false;
                btnEditar.Enabled = false;
                btnBorrar.Enabled = false;
                btnGrabar.Enabled = false;
                btnCancelar.Enabled = true;
            }
        }

    }
}
